{% extends "base/_base.html" %}
{% load staticfiles %}
{% load tags %}


{% block  title %}{{ view.model_meta.verbose_name }}{% endblock %}

{% block page-content %}

    {% add view.model_meta.app_label ":" view.model_meta.model_name as model_view %}
        {# 当前 app_name 可能和 meta.app_label 不同 #}
    {% firstof request.resolver_match.view_name|slice:":-5" model_view as model_view %}
    {% add model_view "_create" as model_view_create %}
    {% add model_view "_detail" as model_view_detail %}
    {% add model_view "_update" as model_view_update %}
    {% add model_view "_delete" as model_view_delete %}

    {% if model_perms.delete %}{% url model_view_delete as objects_delete_url %}{% endif %}

    <div class="row wrapper border-bottom white-bg page-heading">
        <div class="col-lg-10">
            <h2>信息管理</h2>
            <ol class="breadcrumb">
                <li>{% url 'index' as index %}
                    <a href="{% firstof index '/' %}">主页</a>
                </li>
                <li class="active">
                    <strong>{{ view.model_meta.verbose_name }}</strong>
                </li>
            </ol>
        </div>
        <div class="col-lg-2">

        </div>
    </div>
    <div class="row wrapper wrapper-content animated fadeInRight">
        <div class="col-lg-12">

            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>数据列表</h5>
                    <div class="ibox-tools">
                        {% if not view.js_table_data %}
                        <select class="input-sm" id="select_pagesize">

                            <option value="">
                                {% if view.paginate_by %}每页显示{{ view.paginate_by }}条{% else %}显示所有{% endif %}
                            </option>

                            {% for size in view.page_size_list %}
                                {% if size != view.paginate_by %}
                                <option value="{{ size }}">{{ size }}</option>
                                {% endif %}
                            {% endfor %}
                        </select>&nbsp;&nbsp;
                        {% endif %}
                    </div>
                </div>
                <div class="ibox-content">

                    <div class="table-responsive">
                        {% if model_perms.create %}{% url model_view_create as obj_create_url %}{% endif %}

                        <div class="col-md-4">
                            {% if obj_create_url %}<a href="{{ obj_create_url }}" class="btn btn-primary">添加</a>{% endif %}
                            {% if objects_delete_url %}<a class="btn btn-danger">批量删除</a>{% endif %}
                        </div>
                        {% if view.filter_orm %}{% block filter-orm %}
                        <!-- 自定义ORM搜索框列表 -->
                        {% endblock %}{% endif %}
                        <div class="col-md-8 form-inline">
                            {% if view.filter_fields %}
                            <!-- 通用字段搜索框 -->
                            <div class="form-group pull-right">
                                <label class="control-label" for="quantity">搜索/过滤:</label>
                                <input type="text" class="form-control"
                                 name="s" value="{{ request.GET.s }}" 
                                 placeholder="{{ view.filter_labels|join:', ' }}"
                                 title="{{ view.filter_labels|join:', ' }}"
                                 onkeydown="if((event.keyCode==13)&amp;&amp;(this.value!=''))window.location='?s='+this.value.replace(/^\s+|\s+$/g,'');"
                                />
                                <!-- <button class="btn btn-success btn-circle btn-outline" type="button" id="copy" title="查找过滤"><i class="fa fa-search"></i></button> -->
                                {% if request.GET.s %}<a href="?">清空搜索</a>{% endif %}
                            </div>
                            {% endif %}
                        </div>
                        <form id="list_object_form" class="form-horizontal  ">

                        {% block list_table %}
                            <!-- 列表页数据 -->

                            <table class="table table-striped table-bordered table-hover {% if view.js_table_data %}dataTables-example{% endif %}">
                                <thead>
                                <tr>
                                    {% if objects_delete_url %}<th width="20"><input type="checkbox" id="CheckedAll"></th>{% endif %}

                                    {% for field_info in view.list_fields %}
                                        <th>{{ field_info.1 }}</th>
                                    {% endfor %}
                                    {% block add_table_th %}
                                        <!-- 增加自定义th列 -->
                                    {% endblock %}
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>

                                {% for object in object_list %}
                                    {% if model_perms.detail %}{% url model_view_detail object.pk as obj_detail_url %}{% endif %}
                                    {% if model_perms.update %}{% url model_view_update object.pk as obj_update_url %}{% endif %}
                                    <tr id="{{ object.pk }}">
                                        {% if objects_delete_url %}<td><input type="checkbox" value="{{ object.pk }}"  name="id"></td>{% endif %}

                                        {% for field in view.list_fields %}
                                            {% if forloop.first and obj_detail_url %}
                                                <td><a href="{{ obj_detail_url }}">{{ object|lookup_val:field }}</a></td>
                                            {% else %}
                                                <td>{{ object|lookup_val:field }}</td>
                                            {% endif %}
                                        {% endfor %}

                                        {% block add_table_td %}
                                            <!-- 增加自定义td列, 比如虚拟关联model数据 -->
                                        {% endblock %}

                                        <td>
                                            {% if obj_update_url %}<a class="btn btn-info btn-xs" href="{{ obj_update_url }}">编辑</a>{% endif %}

                                            {% if objects_delete_url %}<a class="btn btn-danger btn-xs">删除</a>{% endif %}
                                            {% block actions %}
                                            {% endblock %}

                                        </td>

                                    </tr>
                                {% endfor %}

                                    {% block add_table_row %}
                                    {% endblock %}

                                </tbody>

                            </table>

                        {% endblock %}

                        </form>

                        {% if is_paginated %}

                            {% if request.GET.s %}
                                {% add "s=" request.GET.s as s %}
                            {% endif %}
                            {% if url_args %}{% firstof url_args|join:"&" or s as url_args %}{% endif %}
                            {% firstof view.page_kwarg 'page' as page %}

                        <ul class="pagination pull-right">
                            {% if page_obj.has_previous %}
                                <li><a href="?&{{ url_args }}" title="第一页">«</a></li>
                                <li><a href="?{{ page }}={{ page_obj.previous_page_number }}&{{ url_args }}" title="上一页">‹</a></li>
                            {% endif %}

                            {% for p in page_range %}
                                {% if page_obj.number == p %}
                                <li class="active"><span>{{ p }}</span></li>
                                {% else %}
                                <li><a href="?{{ page }}={{ p }}&{{ url_args }}">{{ p }}</a></li>
                                {% endif %}
                            {% endfor %}

                            {% if page_obj.has_next %}
                                <li><a href="?{{ page }}={{ page_obj.next_page_number }}&{{ url_args }}" title="下一页">›</a></li>
                                {% if paginator.num_pages < 200 %}
                                <li><a href="?{{ page }}=last&{{ url_args }}" title="最末页">»</a></li>
                                {% else %}
                                <!-- 普通分页SQL偏移查询方式(LIMIT/OFFSET)对超大数据支持不好，应改用游标分页 -->
                                {% endif %}
                                <li><span>共{{ paginator.num_pages }}页 {{ paginator.count }}条</span></li>
                            {% endif %}
                        </ul>
                        {% endif %}


                    </div>

                </div>
            </div>
        </div>
    </div>

{% endblock %}



{% block footer-js %}

    <script src="{% static 'js/my.js' %}"></script>

    <script>
        $(function () {

            $('.btn-danger').click(function () {
                // 删除model表obj数据
                if (this.text == '删除' || this.text == '批量删除') {
                    DeleteObj(this)
                }
            });

            $("#select_pagesize").change(function () {
                // 用户改变PageSize
                if (this.value && this.value != "{{ view.paginate_by }}") {
                    window.location.href = "?s={{ request.GET.s }}&{{ view.page_size_kwarg }}=" + this.value;
                }

            });

        });

    </script>


    {% block list_js %}
        <!-- 列表页js扩展 -->
    {% endblock %}

{% endblock %}
